<template>
	<view class="my-recipes">
		<!-- 顶部筛选 -->
		<view class="filter-section">
			<view 
				v-for="(item, index) in mealTypes" 
				:key="index"
				@tap="switchType(item)"
				class="filter-item"
				:style="{color: currentType === item ? '#42bd56' : '#666', fontWeight: currentType === item ? 'bold' : 'normal'}"
			>
				{{item}}
			</view>
		</view>
		
		<!-- 菜谱列表 -->
		<view class="recipe-list">
			<view class="recipe-item" v-for="(item, index) in filteredRecipes" :key="item.id">
				<view class="recipe-content">
					<view class="recipe-info">
						<text class="recipe-name">{{item.menuName}}</text>
						<text class="meal-time" 
							:style="{
								background: item.mealTime === '早餐' ? '#e8f4ff' : item.mealTime === '午餐' ? '#fff3e8' : '#ffeef0',
								color: item.mealTime === '早餐' ? '#42bd56' : item.mealTime === '午餐' ? '#ff9500' : '#ff2d55'
							}"
						>
							{{item.mealTime}}
						</text>
					</view>
					<text class="submit-time">{{item.submitTime}}</text>
				</view>
				<view class="recipe-actions">
					<button class="delete-btn" @tap="deleteRecipe(index, item)">删除</button>
				</view>
			</view>
			
			<!-- 空状态 -->
			<view class="empty-state" v-if="filteredRecipes.length === 0">
				<text>{{ currentType === '全部' ? '暂无菜谱' : `暂无${currentType}菜谱` }}</text>
			</view>
		</view>
		
		<!-- 底部统计 -->
		<view class="stats-section">
			<view class="stat-item">
				<text class="stat-num">{{totalRecipes}}</text>
				<text class="stat-label">总菜谱</text>
			</view>
			<view class="stat-item">
				<text class="stat-num">{{breakfastCount}}</text>
				<text class="stat-label">早餐</text>
			</view>
			<view class="stat-item">
				<text class="stat-num">{{lunchCount}}</text>
				<text class="stat-label">午餐</text>
			</view>
			<view class="stat-item">
				<text class="stat-num">{{dinnerCount}}</text>
				<text class="stat-label">晚餐</text>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				recipes: [],
				currentType: '全部',
				mealTypes: ['全部', '早餐', '午餐', '晚餐']
			}
		},
		computed: {
			filteredRecipes() {
				if (this.currentType === '全部') {
					return this.recipes
				}
				return this.recipes.filter(recipe => recipe.mealTime === this.currentType)
			},
			totalRecipes() {
				return this.recipes.length
			},
			breakfastCount() {
				return this.recipes.filter(recipe => recipe.mealTime === '早餐').length
			},
			lunchCount() {
				return this.recipes.filter(recipe => recipe.mealTime === '午餐').length
			},
			dinnerCount() {
				return this.recipes.filter(recipe => recipe.mealTime === '晚餐').length
			}
		},
		onShow() {
			this.loadRecipes()
		},
		methods: {
			loadRecipes() {
				try {
					const savedRecipes = uni.getStorageSync('meals')
					if (savedRecipes) {
						this.recipes = JSON.parse(savedRecipes)
						this.recipes.sort((a, b) => new Date(b.submitTime) - new Date(a.submitTime))
					}
				} catch (e) {
					console.error('加载菜谱失败:', e)
					uni.showToast({
						title: '加载失败',
						icon: 'none'
					})
				}
			},
			
			switchType(type) {
				this.currentType = type
			},
			
			deleteRecipe(index, recipe) {
				uni.showModal({
					title: '提示',
					content: '确定要删除这个菜谱吗？',
					success: (res) => {
						if (res.confirm) {
							try {
								this.recipes = this.recipes.filter(item => item.id !== recipe.id)
								uni.setStorageSync('meal_records', JSON.stringify(this.recipes))
								
								uni.showToast({
									title: '删除成功',
									icon: 'success'
								})
							} catch (e) {
								console.error('删除失败:', e)
								uni.showToast({
									title: '删除失败',
									icon: 'none'
								})
							}
						}
					}
				})
			}
		}
	}
</script>

<style>
.my-recipes {
	min-height: 100vh;
	background: #f5f5f5;
	padding-bottom: 120rpx;
}

.filter-section {
	position: sticky;
	top: 0;
	z-index: 100;
	display: flex;
	background: #fff;
	padding: 20rpx;
	margin-bottom: 20rpx;
	box-shadow: 0 2rpx 10rpx #42bd56;
}

.filter-item {
	flex: 1;
	text-align: center;
	padding: 15rpx 0;
	font-size: 28rpx;
}

.recipe-list {
	padding: 20rpx;
}

.recipe-item {
	background: #fff;
	border-radius: 12rpx;
	padding: 30rpx;
	margin-bottom: 20rpx;
	display: flex;
	justify-content: space-between;
	align-items: center;
}

.recipe-content {
	flex: 1;
	margin-right: 20rpx;
}

.recipe-info {
	display: flex;
	align-items: center;
	margin-bottom: 10rpx;
}

.recipe-name {
	font-size: 32rpx;
	color: #333;
	font-weight: bold;
	margin-right: 20rpx;
}

.meal-time {
	padding: 4rpx 16rpx;
	border-radius: 20rpx;
	font-size: 24rpx;
}

.submit-time {
	font-size: 24rpx;
	color: #999;
}

.delete-btn {
	font-size: 24rpx;
	padding: 10rpx 30rpx;
	background: #ff5a5f;
	color: #fff;
	border-radius: 30rpx;
	margin: 0;
}

.empty-state {
	padding: 100rpx 0;
	text-align: center;
	font-size: 28rpx;
	color: #999;
}

.stats-section {
	position: fixed;
	bottom: 0;
	left: 0;
	right: 0;
	background: #fff;
	display: flex;
	padding: 20rpx;
	box-shadow: 0 -2rpx 10rpx #42bd56;
}

.stat-item {
	flex: 1;
	text-align: center;
}

.stat-num {
	font-size: 36rpx;
	font-weight: bold;
	color: #42bd56;
	display: block;
	margin-bottom: 8rpx;
}

.stat-label {
	font-size: 24rpx;
	color: #666;
}
</style>